<template>
    <div class="info-collect">
        <Crumb name="Sparepart Quotation"/>
        <div class="content">
            <el-button
                    type="primary"
                    @click="openAdd()"
            >Add New Quotation
            </el-button>
            <Search>
                <el-input
                        v-model="key"
                        size="small"
                        class="input"
                        placeholder="Search By Quotation No"
                        prefix-icon="el-icon-search"
                />
                <el-button
                        size="small"
                        type="primary"
                >Search
                </el-button>
            </Search>
            <div class="table-wrapper">
                <el-table
                        :data="quotationList.slice((pageNum-1)*pageSize,pageNum*pageSize)"
                        border
                        stripe
                        style="width: 100%">
                    <el-table-column
                            prop="quotationNo"
                            label="Quotation Number"
                            width="180"
                    >
                    </el-table-column>
                    <el-table-column
                            prop="quotationDate"
                            label="Quotation Date"
                            width="180">
                    </el-table-column>
                    <el-table-column
                            prop="quotationBy"
                            label="Request By"
                            width="180">
                    </el-table-column>
                    <el-table-column
                            prop="customer"
                            label="Customer"
                            width="250">
                    </el-table-column>
                    <el-table-column
                            prop="statusDesc"
                            label="Status"
                            width="150">
                    </el-table-column>
                    <el-table-column label="Opr">
                        <template slot-scope="scope">
                            <el-button
                                    size="mini"
                                    @click="openEdit(scope.row)">Detail
                            </el-button>
                        </template>
                    </el-table-column>
                </el-table>
                <div class="page">
                    <el-pagination background
                                   :page-size="pageSize"
                                   @current-change="changePage"
                                   layout="prev, pager, next"
                                   :total="total"/>
                </div>

            </div>

            <div class="modal-wrapper">
                <el-dialog
                        title="Sparepart Quotation"
                        :visible.sync="dialogVisible"
                        width="80%"
                        top="0px"
                >
                    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="150px" class="demo-ruleForm"
                             :inline="true">
                        <el-form-item label="Request No." prop="quotationNo">
                            <el-input class="input" v-model="ruleForm.quotationNo"/>
                        </el-form-item>
                        <el-form-item label="Request Date" prop="quotationDateStr">
                            <el-date-picker class="input" v-model="ruleForm.quotationDateStr"
                                            value-format="yyyy-MM-dd"/>
                        </el-form-item>
                        <el-form-item label="Request By" prop="quotationBy">
                            <el-select class="input" v-model="ruleForm.quotationBy">
                                <el-option :value="item.id" :key="item.id" v-for="item in employeeList"
                                           :label="item.employeeName"/>
                            </el-select>
                        </el-form-item>
                        <el-form-item label="Customer" prop="customer">
                            <el-select class="input" v-model="ruleForm.customer">
                                <el-option :value="item.customerId" :key="item.customerId" v-for="item in customerList"
                                           :label="item.customerName"/>
                            </el-select>
                        </el-form-item>
                    </el-form>
                    <div class="items">
                        <span>items:</span>
                        <label for="file">Upload items</label>
                        <el-input size="small" id="file" style="display: none" type="file">Upload items</el-input>
                        <el-button size="small" type="primary">Check part catalog</el-button>
                        <el-button size="small" type="primary" @click="showDB">Add Items</el-button>
                        <div class="table">
                            <Data-base @deleteItem="deleteItem" :partList="partList"/>
                        </div>
                    </div>
                    <span slot="footer" class="dialog-footer">
                        <el-button @click="dialogVisible = false">Cancel</el-button>
                        <el-button type="warning" @click="saveForm">Save</el-button>
                        <el-button type="primary" @click="submitForm">Confirm</el-button>
                    </span>
                </el-dialog>
                <Part-item @close="dbVisible = false" @add="add" :visible="dbVisible"
                />
            </div>
        </div>
    </div>
</template>

<script>
    import Crumb from '@/components/common/crumb'
    import Search from '@/components/common/search'
    import DataBase from '@/components/part/spDataBase'
    import PartItem from '@/components/part/sparePartItem'

    import {
        getQuotationList,
        getQuotationNo,
        getQuotationDetail,
        saveQuotation,
        confirmQuotation
    } from "../../api/quotation";
    import {mapActions, mapGetters} from 'vuex'
    import {ERROR_OK} from "../../util/config";
    import dbMixins from '../../util/spMixins'
    import mixins from '../../util/mixins'

    export default {
        name: "part-quotation",
        mixins: [dbMixins, mixins],
        data() {
            return {
                list: [],
                ruleForm: {
                    quotationNo: '',
                    quotationDateStr: '',
                    quotationBy: '',
                    customer: '',
                    status: ''
                },
                rules: {
                    quotationNo: [
                        {required: true, message: 'Quotation No is required', trigger: 'blur'},
                    ],
                    quotationBy: [
                        {required: true, message: 'Quotation By is required', trigger: 'change'},
                    ],
                    customer: [
                        {required: true, message: 'Name is required', trigger: 'change'}
                    ],
                    quotationDateStr: [
                        {required: false}
                    ]
                },
                key: '',
                dialogVisible: false
            }
        },
        computed: {
            ...mapGetters(['customerList', 'employeeList']),
            quotationList() {
                return this.list.filter(quotation => quotation.quotationNo.indexOf(this.key) > -1)
            }
        },
        methods: {
            showDB() {
                this.dbVisible = true
            },
            async saveForm() {
                this.$refs.ruleForm.validate(async valid => {
                    if (valid) {
                        const quotationDetail = JSON.stringify(this.partList.map(item => {
                            return {partId: item.id, quantity: item.number}
                        }));
                        const {quotationNo, quotationBy, quotationDateStr, customer} = this.ruleForm;
                        let {status, msg} = await saveQuotation({
                            quotationNo,
                            quotationBy,
                            quotationDateStr,
                            customer,
                            quotationDetail
                        });
                        if (status === ERROR_OK) {
                            this.$message({
                                message: 'save Successfully',
                                type: 'success'
                            });
                            this._getQuotationList()
                        } else {
                            this.$notify({
                                title: 'Error',
                                message: msg
                            })
                        }
                        this.dialogVisible = false;
                    }
                });
            },
            async submitForm() {
                this.$refs.ruleForm.validate(async valid => {
                    if (valid) {
                        const quotationDetail = JSON.stringify(this.partList.map(item => {
                            return {partId: item.id, quantity: item.number}
                        }));
                        const {quotationNo, quotationBy, quotationDateStr, customer} = this.ruleForm;
                        let {status, msg} = await confirmQuotation({
                            quotationNo,
                            quotationBy,
                            quotationDateStr,
                            customer,
                            quotationDetail
                        });
                        if (status === ERROR_OK) {
                            this.$message({
                                message: 'confirm Successfully',
                                type: 'success'
                            });
                            this._getQuotationList()
                        } else {
                            this.$notify({
                                title: 'Error',
                                message: msg
                            })
                        }
                        this.dialogVisible = false;
                    }
                });
            },
            clear() {
                this.ruleForm.quotationBy = '';
                this.ruleForm.customer = '';
                // this.ruleForm.quotationDateStr = '';
                this.partList = []
            },
            openAdd() {
                this.clear();
                this._getQuotationNo();
                this.dialogVisible = true
            },
            async openEdit(r) {
                let {status, data} = await getQuotationDetail(r.id);
                if (status === ERROR_OK) {
                    this.ruleForm = data;
                    if (data.spareQuotationDetailVoList) {
                        this.partList = data.spareQuotationDetailVoList.map(item => {
                            return {
                                ...item, number: item.quantity, partsNumber: item.partNo
                            }
                        })
                    } else {
                        this.partList = []
                    }

                }
                this.dialogVisible = true

            },
            async _getQuotationList() {
                let {status, data} = await getQuotationList();
                if (status === ERROR_OK) {
                    this.list = data;
                    this.total = data.length
                }
            },
            async _getQuotationNo() {
                const {status, data} = await getQuotationNo();
                if (status === ERROR_OK) {
                    this.ruleForm.quotationNo = data
                }
            },
            ...mapActions({
                getCustomer: 'loadCustomerList',
                getEmployee: 'loadEmployeeList'
            })
        },
        watch: {
            quotationList(list) {
                this.total = list.length
            }
        },
        created() {
            this._getQuotationList();
            this.getCustomer();
            this.getEmployee();
        },
        components: {
            Crumb, Search, DataBase, PartItem
        }
    }
</script>

<style scoped lang="scss">
    @import "../../css/mixins";

    .content {
        @include content
    }

    .modal-wrapper {
        .input {
            width: 200px;
        }
        .items {
            span {
                display: inline-block;
                width: 140px;
                text-align: right;
                font-size: 16px;
                margin-right: 10px;
            }
            label {
                width: 142px;
                height: 32px;
                display: inline-block;
                text-align: center;
                color: #fff;
                margin-right: 20px;
                cursor: pointer;
                line-height: 32px;
                border-radius: 3px;
                background: #409Eff;
            }
            .table {
                margin-top: 5px;
            }
        }
    }

</style>